<?php
/**
 * @file ct.comic.todirectory.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-01-01 04:40:44 +0000 (Tue, 01 Jan 2013) $
 *
 * $Id: ct.comic.todirectory.php 48 2013-01-01 04:40:44Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/cron/ct.comic.todirectory.php $
 */
class WCComicsToDirectory extends WCCronTool {
	public function run($spacer="") {
		global	$wcOptions;

		$log = WCLog::Get(WC_LOGNAME_CRON);
		if(!$log->isOk()) {
			$log->set();
		}

		$moved = false;

		if($wcOptions->activated("Group") && $wcOptions->activated("Comic") && $wcOptions->activated("Directory")) {
			$ok = true;

			global	$wcGroupsHolder;

			$groupId	= $wcOptions->value("Group");
			$comicId	= $wcOptions->value("Comic");
			$directoryId	= $wcOptions->value("Directory");

			// Checks.
			// @{
			if($ok) {
				$group = $wcGroupsHolder->groupById($groupId);
				if(!$group) {
					$ok = false;
					$log->write("Error: Invalid group id '{$groupId}'.\n", WCLogLevel::Fatal);
				}
			}
			if($ok) {
				global	$wcDirectoriesHolder;

				$directory = $wcDirectoriesHolder->item($directoryId, $group);
				if(!$directory || !$directory->ok()) {
					$ok = false;
					$log->write("Error: Invalid directory id '{$directoryId}' in group '{$group->id}'.\n", WCLogLevel::Fatal);
				}
			}
			if($ok) {
				global	$wcComicsHolder;
					
				$comic = $wcComicsHolder->item($comicId, $group);
				if(!$comic || !$comic->ok()) {
					$ok = false;
					$log->write("Error: Invalid comic id '{$comicId}' in group '{$group->id}'.\n", WCLogLevel::Fatal);
				}
			}
			if($ok && $comic->directory == $directory->id) {
				$ok = false;
				$log->write("Error: Comic '{$comic->id}' is already in directory '{$directory->id}'.\n", WCLogLevel::Error);
			}
			// @}

			if($ok) {
				// Summary.
				$log->write("{$spacer}Group:     '{$group->name}' ({$group->id})\n");
				$log->write("{$spacer}\troot: '{$group->root}'\n");
				$log->write("{$spacer}Comic:     '{$comic->name}' ({$comic->id})\n");
				$log->write("{$spacer}\tat:   '{$comic->directory()->name}' ({$comic->directory})\n");
				$log->write("{$spacer}Directory: '{$directory->name}' ({$directory->id})\n");

				if(!$comic->setDirectory($directory)) {
					$ok = false;
					$log->write("Error: Unable to move '{$comic->id}' to directory '{$directory->id}'.\n", WCLogLevel::Fatal);
				}
			}
		} else {
			$this->showHelp($spacer);
		}

		return $moved;
	}
	public function showHelp($spacer="") {
		echo "{$spacer}This tool moves a comic to a directory.\n\n";
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-c id, --comic id\n";
		echo "{$spacer}\t\tComic id.\n\n";
		echo "{$spacer}\t-d id, --directory id\n";
		echo "{$spacer}\t\tDirectory id.\n\n";
		echo "{$spacer}\t-g id, --group id\n";
		echo "{$spacer}\t\tGroup id.\n";
	}

	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global	$wcOptions;

			if($wcOptions->addValueOption("Group")) {
				$wcOptions->addOptionTrigger("Group", "g");
				$wcOptions->addOptionTrigger("Group", "group");
			}
			if($wcOptions->addValueOption("Comic")) {
				$wcOptions->addOptionTrigger("Comic", "c");
				$wcOptions->addOptionTrigger("Comic", "comic");
			}
			if($wcOptions->addValueOption("Directory")) {
				$wcOptions->addOptionTrigger("Directory", "d");
				$wcOptions->addOptionTrigger("Directory", "directory");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}
?>